package com.agriculture.utils;

import com.agriculture.entity.Seller;
import com.agriculture.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;



public class JwtUtils {
    // 设置密钥
    private static String signKey = "agriculture";
    // 设置过期时间为 12 h
    private static Long expire = 43200000L;

    /**
     * 生成 JWT 令牌
     * @param claims
     * @return
     */

    public static String generateJwt(Map<String, Object> claims){
        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256,signKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
        return jwt;
    }

    /**
     * 解析令牌
     * @param jwt
     * @return
     */

    public static Claims parseJWT(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }


    /**
     * 将 token 转为 User 类
     * @param token
     * @return
     */
    public static User parseToken(String token) {
        // 解析 token
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(token)
                .getBody();

        // 获取 id
        Long id = claims.get("id", Long.class);
        // 获取 name
        String name = claims.get("name", String.class);
        // 获取 phone
        String phone = claims.get("phone", String.class);
        String sex = claims.get("sex", String.class);
        String idNumber = claims.get("id_number", String.class);
        String avatar = claims.get("avatar", String.class);
        Integer isDelete = claims.get("is_delete", Integer.class);

        // 创建 User 对象
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPhone(phone);
        user.setSex(sex);
        user.setIdNumber(idNumber);
        user.setAvatar(avatar);
        user.setIsDeleted(isDelete);

        return user;
    }

    public static Seller parseToken_seller(String token) {
        // 解析 token
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(token)
                .getBody();

        // 获取 id
        Long id = claims.get("id", Long.class);
        // 获取 name
        String name = claims.get("name", String.class);
        // 获取 phone
        String phone = claims.get("phone", String.class);
        String sex = claims.get("sex", String.class);
        String idNumber = claims.get("id_number", String.class);
        String avatar = claims.get("avatar", String.class);
        Integer isDelete = claims.get("is_delete", Integer.class);

        // 创建 User 对象
        Seller seller = new Seller();
        seller.setId(id);
        seller.setName(name);
        seller.setPhone(phone);
        seller.setSex(sex);
        seller.setIdNumber(idNumber);
        seller.setAvatar(avatar);
        seller.setIsDeleted(isDelete);

        return seller;
    }

}
